package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.codec.Base64;
import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ims.v20201229.ImsClient;
import com.tencentcloudapi.ims.v20201229.models.ImageModerationRequest;
import com.tencentcloudapi.ims.v20201229.models.ImageModerationResponse;
import com.tencentcloudapi.tms.v20201229.TmsClient;
import com.tencentcloudapi.tms.v20201229.models.TextModerationRequest;
import com.tencentcloudapi.tms.v20201229.models.TextModerationResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


@Slf4j
@Service
public class VodServiceImpl implements VodService {

    @Autowired
    private VodConstantProperties vodConstantProperties;

    @Autowired
    private Credential credential;

    /**
     * 文本审核
     *
     * @param content
     * @return
     */
    @Override
    public String scanText(String content) {
        try {
            //1.实例化要请求产品的client对象,clientProfile是可选的
            TmsClient client = new TmsClient(credential, vodConstantProperties.getRegion());
            //2.实例化一个请求对象,每个接口都会对应一个request对象
            TextModerationRequest req = new TextModerationRequest();
            //3.对审核内容进行Base64编码
            req.setContent(Base64.encode(content));
            //4.返回的resp是一个TextModerationResponse的实例，与请求对象对应
            TextModerationResponse resp = client.TextModeration(req);
            //获取审核结果建议
            if (resp != null) {
                String suggestion = resp.getSuggestion();
                return suggestion.toLowerCase();
            }
        } catch (TencentCloudSDKException e) {
            log.error("[专辑服务]文本审核异常：{}", e);
        }
        return null;
    }

    /**
     * 图片同步审核
     *
     * @param file 图片文件
     * @return
     */
    @Override
    public String scanImage(MultipartFile file) {
        try {
            //1.实例化要请求产品的client对象,clientProfile是可选的
            ImsClient client = new ImsClient(credential, vodConstantProperties.getRegion());
            //2.实例化一个请求对象,每个接口都会对应一个request对象
            ImageModerationRequest req = new ImageModerationRequest();
            //2.1 对图片进行Base64编码
            req.setFileContent(Base64.encode(file.getInputStream()));
            //3.返回的resp是一个ImageModerationResponse的实例，与请求对象对应
            ImageModerationResponse resp = client.ImageModeration(req);
            // 输出json格式的字符串回包
            if (resp != null) {
                String suggestion = resp.getSuggestion();
                return suggestion.toLowerCase();
            }
        } catch (Exception e) {
            log.error("[专辑服务]图片同步审核异常：{}", e);
        }
        return null;
    }

    @Autowired
    private AlbumInfoMapper albumInfoMapper;


    /**
     * SpringBoot提供异步任务（开启子线程执行）
     * 异步任务审核
     *
     * @param albumInfo
     */
    @Async
    public void reviewAlbumText(AlbumInfo albumInfo) {
        log.info("{}，执行审核。", Thread.currentThread().getName());
        String suggest = this.scanText(albumInfo.getAlbumTitle() + albumInfo.getAlbumIntro());
        if (suggest.equals("pass")) {
            albumInfo.setStatus(SystemConstant.ALBUM_STATUS_PASS);
        }else{
            albumInfo.setStatus(SystemConstant.ALBUM_STATUS_NO_PASS);
        }
        albumInfoMapper.updateById(albumInfo);
    }


}
